package cn.kgc.layui.servlet;

import cn.hutool.json.JSONUtil;
import cn.kgc.layui.entity.User;
import cn.kgc.layui.service.UserService;
import cn.kgc.layui.service.impl.UserServiceImpl;
import cn.kgc.layui.utils.LayResult;
import cn.kgc.layui.utils.WriteJsonUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author: mosin
 * @version: v1.0  2022/9/15
 */
@WebServlet("/user")
public class UserServlet extends HttpServlet {
    private UserService userService  = new UserServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String method = req.getParameter("method");
        switch (method){
            case "selectPage":
                selectPage(req,resp);
                break;
            case "add":
                add(req,resp);
                break;
            case "update":
                update(req,resp);
                break;
            case "delete":
                delete(req,resp);
                break;
            case "deleteMore":
                deleteMore(req,resp);
                break;
        }

    }

    //批量删除
    private void deleteMore(HttpServletRequest req, HttpServletResponse resp)throws IOException {

        String ids = req.getParameter("ids"); //  1001,1002,1003
        System.out.println("ids = " + ids);
        String[] idsArray = ids.split(",");
        LayResult result  = userService.deleteMore(idsArray);
        WriteJsonUtil.writeJson(resp, result);

    }

    private void delete(HttpServletRequest req, HttpServletResponse resp)throws IOException {
        // 获取删除用户的id
        String id = req.getParameter("id");
        System.out.println("id = " + id);
         LayResult result =  userService.delete(id);
         WriteJsonUtil.writeJson(resp, result);
    }

    // 修改用户信息
    private void update(HttpServletRequest req, HttpServletResponse resp)throws IOException {
        //获取添加用户的信息
        String user = req.getParameter("user");
        User usr = JSONUtil.toBean(user, User.class);
        //添加用户信息
        LayResult  result =  userService.update(usr);
        //响应数据
        WriteJsonUtil.writeJson(resp, result);

    }

    private void add(HttpServletRequest req, HttpServletResponse resp)throws IOException {

        //获取添加用户的信息
        String user = req.getParameter("user");
        User usr = JSONUtil.toBean(user, User.class);
        //添加用户信息
        LayResult  result =  userService.add(usr);
        //响应数据
        WriteJsonUtil.writeJson(resp, result);
    }

    //分页查询
    private void selectPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        // 获取分页参数  page limit
        Integer page = Integer.valueOf(req.getParameter("page"));
        Integer limit = Integer.valueOf(req.getParameter("limit"));

        String searchParams = req.getParameter("searchParams");  //  user 对象   json字符串
        //user json字符串   user java 对象
        User user = null;
        if(searchParams!=null){
            user = JSONUtil.toBean(searchParams, User.class);
        }

        LayResult layResult =  userService.selectPage(page,limit,user);
        //将响应数据转为json   写出
        WriteJsonUtil.writeJson(resp, layResult);
    }
}
